<?php
class Default_Form_Validate_Users_Addnew{
	
	//CHUA NHUNG THONG BAO LOI CUA FORM
	protected $_messagesError = null;
	
	//MANG CHUA DU LIEU SAU KHI KIEM TRA
	protected  $_arrData;
	
	public function __construct($arrParam = array(), $options = null){
		
		//=========================================
		//KIEM TRA USER NAME
		//==========================================
		$validator = new Zend_Validate();
		$option    = array('table'=>'users','field'=>'user_name');
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_StringLength(4,32))
				  ->addValidator(new Zendvn_Validate_Regex('#^[a-zA-Z0-9\-_\s\.]+$#'),true)
				  ->addValidator(new Zendvn_Validate_Db_NoRecordExists($option),true);
		if(!$validator->isValid($arrParam['user_name'])){
			$message = $validator->getMessages();
			$this->_messagesError['user_name'] = 'Tên đăng nhập: '.current($message);
			$arrParam['user_name']='';
		}
		
		//=========================================
		//KIEM TRA  ANH Avatar
		//==========================================
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo  = $upload->getFileInfo('user_avatar');
		$fileName  = $fileInfo['user_avatar']['name'];
		if(!empty($fileName)){
			$upload->addValidator('Extension',true,array('jpg','png','gif'),'user_avatar')
				   ->addValidator('Size',true,array('min'=>'10KB','max'=>'1000KB'),'user_avatar');
			if(!$upload->isValid('user_avatar')){
				$message = $upload->getMessages();
				$this->_messagesError['user_avatar']= 'Ảnh đại diện:' . current($message);
			}
		}
		//=========================================
		//KIEM TRA PASSWORD
		//==========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				 ->addValidator(new Zendvn_Validate_StringLength(4,32))
				 ->addValidator(new Zendvn_Validate_Regex('#^[a-zA-Z0-9\-_\s\.]+$#'),true);
		if(!$validator->isValid($arrParam['password'])){
				$message = $validator->getMessages();
				$this->_messagesError['password'] = 'Mật khẩu: '.current($message);
				$arrParam['password']='';
		}
		//=========================================
		//KIEM TRA EMAIL
		//==========================================
		$validator = new Zend_Validate();
		$option    = array('table'=>'users','field'=>'email');
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_EmailAddress(),true)
				  ->addValidator(new Zendvn_Validate_Db_NoRecordExists($option),true);
		if(!$validator->isValid($arrParam['email'])){
			$message = $validator->getMessages();
			$this->_messagesError['email'] = 'Email: ' . current($message);
			$arrParam['email'] = '';
		}
		//========================================
		// KIEM TRA slbGroup
		//========================================
		if($arrParam['slbGroup'] == 0){
		$this->_messagesError['slbGroup'] = 'Thuộc nhóm: Chưa chọn nhóm';
		}
		
		//=========================================
		//KIEM TRA First Name
		//==========================================
		
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_StringLength(2,32),true);	 
		if(!$validator->isValid($arrParam['first_name'])){
			$message = $validator->getMessages();
			$this->_messagesError['first_name'] = 'Họ và tên đệm: '.current($message);
			$arrParam['first_name']='';
		}
		
		//=========================================
		//KIEM TRA Last Name
		//==========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_StringLength(1,32),true);	 
		if(!$validator->isValid($arrParam['last_name'])){
			$message = $validator->getMessages();
			$this->_messagesError['last_name'] = 'Tên: '.current($message);
			$arrParam['last_name']='';
		}
		
		//=========================================
		//KIEM TRA Birthday
		//==========================================
		if($arrParam['day'] == 0){
			$this->_messagesError['day'] = 'Chưa chọn ngày sinh';
		}
			
		if($arrParam['month'] == 0){
			$this->_messagesError['month'] = 'Chưa chọn tháng sinh';
		}
		if($arrParam['year'] == 0){
			$this->_messagesError['year'] = 'Chưa chọn năm sinh';
		}
		
		//========================================
		// KIEM TRA address
		//========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
		          ->addValidator(new Zendvn_Validate_StringLength(5),true);
		if(!$validator->isValid($arrParam['address'])){
			$message = $validator->getMessages();
			$this->_messagesError['address'] = 'Địa chỉ: ' . current($message);
			$arrParam['address'] = '';
		}
		//========================================
		// KIEM TRA mobile
		//========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_Digits(),true);
		if(!$validator->isValid($arrParam['mobile'])){
			$message = $validator->getMessages();
			$this->_messagesError['mobile'] = 'Điện thoại: ' . current($message);
			$arrParam['mobile'] = '';
		}
		//=========================================
		//KIEM TRA Status
		//==========================================
		if($arrParam['status'] == 2){
			$this->_messagesError['status'] = 'Chưa chọn trạng thái';
		}
		
	
		//=========================================
		//TRUYEN CAC GIA TRI DUNG VAO $_arrData
		//==========================================
		$this->_arrData = $arrParam;
	}
	
	//KIEM TRA LOI
	//return true neu co loi xay ra
	public function isError(){
		if(count($this->_messagesError)>0){
			return true;
		}else{
			return false;
		}
		
	}
	
	//TRA VE MOT MANG CAC LOI
	public function getMessageError(){
		return $this->_messagesError;
	}
	
	//TRA VE MOT MANG DU LIEU SAU KHI KIEM TRA
	public function getData($options = null){
	 if($options == 'upload'){
	 	$this->_arrData['user_avatar'] = $this->uploadFile();
	 }
		return $this->_arrData;
	}
	
	public function uploadFile(){
		$upload = new Zendvn_File_Upload();
		$fileInfo   = $upload->getFileInfo('user_avatar');
		$fileName   = $fileInfo['user_avatar']['name'];
		if(!empty($fileName)){
			$fileName = $upload->upload('user_avatar',FILES_PATH . '/user/orignal',
										array('task'=>'rename'),'user_'
										);
			$thumb = Zendvn_File_Image::create(FILES_PATH . '/user/orignal/'.$fileName);
			$thumb->resize(100,100)
				  ->save(FILES_PATH . '/user/img100x100/'.$fileName);
				  
			$thumb = Zendvn_File_Image::create(FILES_PATH . '/user/orignal/'.$fileName);
			$thumb->resize(35,35)
			       ->save(FILES_PATH . '/user/img35x35/'.$fileName);
		}
		return $fileName;
	}
}